package com.study.springbootsecurity.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * @Description: TODO
 * @author: Huangjianyun
 * @date: 2025-06-16 15:41
 */
public class DateUtil {
    /**
     * 时间格式常量
     */
    public static final String COMMON_PATTERN       = "yyyy-MM-dd HH:mm:ss";
    public static final String SHORT_PATTERN        = "yyyyMMdd";

    public static Date convert(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
        TimeZone tz = TimeZone.getTimeZone("GMT+8");

        sdf.setTimeZone(tz);
        Date s;
        String da;
        try {
            s = sdf.parse(time);
            System.out.println(s);    //  Sun Oct 22 00:00:00 CST 2017
            sdf = new SimpleDateFormat("yyyy-MM-dd");
            da = sdf.format(s);
            System.out.println(da);   //  2017-10-22
            return sdf.parse(da);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }


    public static String formatDateTime(java.time.LocalDateTime dateTime, String format) {
        java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern(format);
        return dateTime.format(formatter);
    }

    // 获取当天的开始时间 格式为 yyyy-MM-dd HH:mm:ss
    public static String getCurrentStartTime() {
        return getFormattedDateTime(LocalDate.now().atStartOfDay());
    }
    //  获取当天的结束时间 格式为 yyyy-MM-dd HH:mm:ss
    public static String getCurrentEndTime() {
        return getFormattedDateTime(LocalDate.now().atTime(LocalTime.MAX));
    }

    public static String getFormattedDateTime(LocalDateTime dateTime) {
        return getFormattedDateTime(dateTime, COMMON_PATTERN);
    }

    public static String getFormattedDateTime(LocalDateTime dateTime, String format) {
        return dateTime.format(DateTimeFormatter.ofPattern(format));
    }

    public static String getCurrentDateTimeString() {
        return getCurrentDateTimeString(COMMON_PATTERN);
    }

    public static String getShortNowString() {
        return getCurrentDateTimeString(SHORT_PATTERN);
    }

    public static String getCurrentDateTimeString(String pattern) {
        LocalDateTime currentDateTime = LocalDateTime.now();
        final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return currentDateTime.format(formatter);
    }

}
